package com.ruoyi.server.login;

import cn.hutool.core.util.RandomUtil;
import com.ruoyi.server.encrypt.AuthEncryption;
import com.ruoyi.server.utils.OnlineUtil;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.DefaultFullHttpResponse;

import java.time.LocalDateTime;

/**
 *  POL 概念加密写入
 *      TODO 这里需要处理一下加密内容
 *
 * @author OpenACloud
 * @create 2024/6/6 9:29
 */
public class LoginWriter extends ChannelDuplexHandler {

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        if(msg instanceof DefaultFullHttpResponse) {
            int numAuth = RandomUtil.randomBoolean() ? RandomUtil.randomInt(100, 124) : RandomUtil.randomInt(-100, -124);
            DefaultFullHttpResponse rsp = (DefaultFullHttpResponse) msg;
            ctx.attr(OnlineUtil.AUTH_BYTE).set(new Long(numAuth));
            rsp.headers().set("AuthByte2", AuthEncryption.encryption(numAuth));
            ctx.writeAndFlush(rsp, promise);
        }
    }
}
